/**
 * 
 */
package com.ls.lsqc.modules.sys.utils;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ls.lsqc.common.mapper.JsonMapper;
import com.ls.lsqc.common.utils.CacheUtils;
import com.ls.lsqc.common.utils.SpringContextHolder;
import com.ls.lsqc.modules.sys.dao.CodemanagerDao;
import com.ls.lsqc.modules.sys.entity.Codemanager;

/**
  * @ClassName: CodemanagerUtils
  * @Description: 字典工具类
  * 
  * @author 邱明欣
  * @date 2016-10-10 下午3:49:50
  *
  */

public class CodemanagerUtils {
	
	private static CodemanagerDao codemanagerDao = SpringContextHolder.getBean(CodemanagerDao.class);

	public static final String CACHE_CODE_MANAGER_MAP = "CodemanagerMap";

	/**
	  * @Title: putCodemanagerCache
	  * @Description: 数据字典全量放入缓存
	  * @author 邱明欣 
	  * @date 2016-10-10 下午4:37:42
	  *
	  *
	  * @return void 
	  * @throws
	  */
	
	public static void putCodemanagerCache(){
		@SuppressWarnings("unchecked")
		Map<String, List<Codemanager>> codemanagerMap = (Map<String, List<Codemanager>>)CacheUtils.get(CACHE_CODE_MANAGER_MAP);
		if (codemanagerMap==null){
			codemanagerMap = Maps.newHashMap();
			for (Codemanager codemanager : codemanagerDao.findAllList(new Codemanager())){
				List<Codemanager> codemanagerList = codemanagerMap.get(codemanager.getManagerCode());
				if (codemanagerList != null){
					codemanagerList.add(codemanager);
				}else{
					codemanagerMap.put(codemanager.getManagerCode(), Lists.newArrayList(codemanager));
				}
			}
			CacheUtils.put(CACHE_CODE_MANAGER_MAP, codemanagerMap);
		}
	}
	
	/**
	  * @Title: getCodemanagerList
	  * @Description: 返回字典列表
	  * @author 邱明欣 
	  * @date 2016-10-10 下午4:48:36
	  *
	  * @param code
	  * @return
	  *
	  * @return List<Codemanager> 
	  * @throws
	  */
	
	public static List<Codemanager> getCodemanagerList(String code){
		@SuppressWarnings("unchecked")
		Map<String, List<Codemanager>> codemanagerMap = (Map<String, List<Codemanager>>)CacheUtils.get(CACHE_CODE_MANAGER_MAP);
		if (codemanagerMap==null){
			codemanagerMap = Maps.newHashMap();
			for (Codemanager codemanager : codemanagerDao.findAllList(new Codemanager())){
				List<Codemanager> codemanagerList = codemanagerMap.get(codemanager.getManagerCode());
				if (codemanagerList != null){
					codemanagerList.add(codemanager);
				}else{
					codemanagerMap.put(codemanager.getManagerCode(), Lists.newArrayList(codemanager));
				}
			}
			CacheUtils.put(CACHE_CODE_MANAGER_MAP, codemanagerMap);
		}
		List<Codemanager> codemanagerList = codemanagerMap.get(code);
		if (codemanagerList == null){
			codemanagerList = Lists.newArrayList();
		}
		return codemanagerList;
	}

	/**
	  * @Title: getDictListJson
	  * @Description: 返回字典列表（JSON）
	  * @author 邱明欣 
	  * @date 2016-10-10 下午4:48:10
	  *
	  * @param code
	  * @return
	  *
	  * @return String 
	  * @throws
	  */
	
	public static String getDictListJson(String code){
		return JsonMapper.toJsonString(getCodemanagerList(code));
	}
}
